<template>
  <div class="app-container">
    <el-form
      :model="formData"
      ref="formData"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="测试文本" prop="chatText">
        <el-input
          v-model="formData.chatText"
          placeholder="模糊查询"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="音色名称" prop="voiceType">
        <el-select
          v-model="formData.voiceType"
          filterable
          placeholder="请选择"
          clearable
        >
          <el-option
            v-for="item in voiceTypeList"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="音高" prop="pitchRatio">
        <el-input
          v-model="formData.pitchRatio"
          style="min-width: 230px"
          placeholder="只能输入数字，小数点保留一位即可"
          clearable
          @input="handleInput($event, 'pitchRatio')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="语速" prop="speedRatio">
        <el-input
          v-model="formData.speedRatio"
          style="min-width: 230px"
          placeholder="只能输入数字，小数点保留一位即可"
          clearable
          @input="handleInput($event, 'speedRatio')"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="datetimerange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="onClickAdd"
          v-hasPermi="['customerService:ttsTestList:add']"
          >新建</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['customerService:ttsTestList:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 表格   -->
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column label="录音名称" align="center" prop="content" />
      <el-table-column label="音色名称" align="center" prop="voiceName" />
      <el-table-column
        label="语速"
        align="center"
        prop="speedRatio"
      ></el-table-column>
      <el-table-column label="音量" align="center" prop="volumeRatio" />
      <el-table-column label="音高" align="center" prop="pitchRatio" />
      <el-table-column label="测试文本" align="center" prop="chatText" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            v-if="scope.row.ttsUrl"
            @click="onAudioDownload(scope.row)"
            type="text"
            >音频下载</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="pagination.pageNum"
      :limit.sync="pagination.pageSize"
      @pagination="getList"
    />
    <add-dialog
      v-if="addDialogShow"
      :addDialogShow="addDialogShow"
      @onClose="onClose"
    ></add-dialog>
  </div>
</template>

<script>
import addDialog from "./components/addDialog.vue";
import { getList } from "@/api/customerService/tts-test-list";
import { listData } from "@/api/system/dict/data";
import mixins from "./common";

export default {
  name: "ttsTestList",
  components: { addDialog },
  mixins: [mixins],
  data() {
    return {
      loading: false,
      tableData: [], //表格数据
      showSearch: true, //显示搜索条件
      // 查询参数
      formData: {
        chatText: "", //测试文本
        voiceType: "", //音色
        pitchRatio: "", //音高
        speedRatio: "", //语速
      },
      voiceTypeList: [], //音色数据
      pagination: {
        pageNum: 1,
        pageSize: 10,
      },
      dateRange: [], //日期范围
      total: 0,
      importShow: false, //导入
      addDialogShow: false,
      voiceTypeList: [],
    };
  },
  async created() {
    await this.getListType();
    await this.getList();
  },
  methods: {
    async getListType() {
      const { rows } = await listData({
        pageNum: 1,
        pageSize: 1000,
        dictType: "sys_voice_type",
      });
      this.voiceTypeList = rows || [];
    },
    // 列表查询
    async getList() {
      const voiceAll = this.voiceTypeList.find(
        (el) => el.dictValue == this.formData.voiceType
      );
      const formData = JSON.parse(JSON.stringify(this.formData));
      delete formData["voiceType"];
      let params = {
        ...formData,
        voiceName: voiceAll?.dictLabel || undefined, //音色
        ...this.pagination,
      };
      this.loading = true;
      const { total, rows } = await getList(
        this.addDateRange(params, this.dateRange)
      );
      this.total = total;
      this.tableData = rows;
      this.loading = false;
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.pagination.pageNum = 1;
      this.getList();
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.formData = {
        chatText: "", //测试文本
        voiceType: "", //音色
        pitchRatio: "", //音高
        speedRatio: "", //语速
      };
      this.dateRange = [];
      this.handleQuery();
    },

    /** 导出按钮操作 */
    handleExport() {
      const url = `system/sysTtsVoice/exportTtsVoice`;
      const voiceAll = this.voiceTypeList.find(
        (el) => el.dictValue == this.formData.voiceType
      );
      const formData = JSON.parse(JSON.stringify(this.formData));
      delete formData["voiceType"];
      let data = {
        ...formData,
        voiceName: voiceAll?.dictLabel || undefined, //音色
        ...this.pagination,
      };
      if (this.dateRange?.length) {
        data.params =
          typeof data.params === "object" &&
          data.params !== null &&
          !Array.isArray(data.params)
            ? data.params
            : {};
        data.params["beginTime"] = this.dateRange[0];
        data.params["endTime"] = this.dateRange[1];
      }
      this.download(
        url,
        {
          ...data,
        },
        `ttsTestList_${new Date().getTime()}.xlsx`
      );
    },

    // 音频下载
    async onAudioDownload(row) {
      console.log(row, "row");
      const fileUrl = row?.ttsUrl;
      // 打开新标签页进行下载
      window.open(fileUrl, "_blank");
    },

    // 新建
    onClickAdd() {
      this.addDialogShow = true;
    },

    // 取消
    onClose() {
      this.addDialogShow = false;
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped></style>
